package server

import (
  "github.com/gin-gonic/gin"
  "reflect"
)

var ResponderList []Responder

func init() {
  ResponderList = []Responder{new(StringResponder), new(ModelResponder)}
}

type Responder interface {
  Res() gin.HandlerFunc
}

func Convert(handlers interface{}) gin.HandlerFunc {
  hr := reflect.ValueOf(handlers)
  for _, r := range ResponderList {
    rr := reflect.ValueOf(r).Elem()
    if hr.Type().ConvertibleTo(rr.Type()) {
      rr.Set(hr)
      return rr.Interface().(Responder).Res()
    }
  }
  return nil
}

type StringResponder func(*gin.Context) string

func (this StringResponder) Res() gin.HandlerFunc {
  return func(c *gin.Context) {
    c.String(200, this(c))
  }
}

type ModelResponder func(*gin.Context) IModel

func (this ModelResponder) Res() gin.HandlerFunc {
  return func(c *gin.Context) {
    c.JSON(200, this(c))
  }
}
